package com.qf.web.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.qf.aop.aspect.ApiOperationLog;
import com.qf.aop.aspect.ApiOperationLogTitle;
import com.qf.api.sys.entity.SysRoleEntity;
import com.qf.api.sys.form.RoleForm;
import com.qf.api.sys.mybatis.page.BaseController;
import com.qf.api.sys.mybatis.page.PageResult;
import com.qf.api.sys.service.SysRoleService;
import com.qf.api.sys.vo.OptionsVO;
import com.qf.api.sys.vo.RolePageVO;
import com.qf.core.enums.common.BusinessTypeEnum;
import com.qf.core.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: sin
 * @Date: 2025/5/8 17:41
 * @Description:
 **/
@Slf4j
@RestController
@Tag(name = "04.角色接口", description = "角色管理相关接口")
@ApiOperationLogTitle("角色模块")
@RequestMapping("/admin/roles")
public class RoleController extends BaseController {

    @Resource
    private SysRoleService roleService;

    @Operation(summary = "01.查询角色列表", description = "返回分页的角色数据")
    @GetMapping
    public ResponseResult<PageResult<RolePageVO>> getRolePage(@RequestParam(value = "name", required = false) String name,
                                                              @RequestParam(value = "status", required = false) Integer status,
                                                              @RequestParam(value = "code", required = false) String code) {
        startPage();
        List<RolePageVO> roles = roleService.getRolePage(name, status, code);
        return ResponseResult.success(getData(roles));
    }

    @Operation(summary = "02.新增角色")
    @ApiOperationLog(desc = "新增角色", businessType = BusinessTypeEnum.ADD)
    @PostMapping
    public ResponseResult<Boolean> saveRole(@RequestBody RoleForm role) {
        SysRoleEntity sysRole = BeanUtil.copyProperties(role, SysRoleEntity.class);
        boolean result = roleService.save(sysRole);
        return isOk(result);
    }

    @Operation(summary = "03.修改角色",description = "根据roleId修改角色信息")
    @ApiOperationLog(desc = "修改角色", businessType = BusinessTypeEnum.MODIFY)
    @PutMapping("/{id}")
    public ResponseResult<Boolean> updateRole(@PathVariable("id") Long id, @RequestBody RoleForm role) {
        boolean result = roleService.updateRoleById(id, role.getName(),role.getCode(),role.getRemark());
        return isOk(result);
    }

    @Operation(summary = "04.删除角色",description = "根据roleId删除角色，支持单个删除，暂不支持批量")
    @ApiOperationLog(desc = "删除角色", businessType = BusinessTypeEnum.DELETE)
    @DeleteMapping("/{id}")
    public ResponseResult<Boolean> deleteRole(@PathVariable("id") Long id) {
        return isOk(roleService.removeById(id));
    }

    @Operation(summary = "05.获取角色下拉列表")
    @GetMapping("/options")
    public ResponseResult<List<OptionsVO>> getRoleOption() {
        List<OptionsVO> list = roleService.getRoleOption();
        return ResponseResult.success(list);
    }

    /**
     * 通过角色Id获取角色菜单权限
     **/
    @Operation(summary = "06.获取角色菜单权限")
    @GetMapping("/{id}/menus")
    public ResponseResult<List<Long>> getRoleMenu(@PathVariable("id") Long roleId) {
        return ResponseResult.success(roleService.getRoleMenuByRoleId(roleId));
    }

    /**
     * 通过角色id修改角色权限
     **/
    @Operation(summary = "07.修改角色权限")
    @ApiOperationLog(desc = "修改角色权限", businessType = BusinessTypeEnum.GRANT)
    @PutMapping("/{id}/menus")
    public ResponseResult<Boolean> updateRoleMenu(@PathVariable(value = "id") Long roleId,
                                                  @RequestBody @NotNull List<Long> menuIds) {
        return isOk(roleService.updateRoleMenu(roleId, menuIds));
    }
}

